<?php


namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class dianpuModel extends Model
{
    use SoftDeletes;

    protected $table = 'dianpu';
    protected $primaryKey = 'id';
    protected $appends = ['status_name'];
    protected $hidden = [];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getStatusNameAttribute($value)
    {
        $status_name = '禁用';

        if ($this->attributes['status'] > 0) {
            $status_name = '正常';
        }

        return $status_name;
    }

    public function dianpuapply()
    {
        return $this->hasOne(dianpuapplyModel::class, 'apply_id', 'id');
    }

    public function member()
    {
        $member_table = (new \memberModel())->getTable();
        $merchant_table = (new merchantModel())->getTable();
        return $this->hasManyThrough(\memberModel::class,
                                     merchantModel::class,
                                     'dianpu_id',
                                     'id',
                                     'id',
                                     'member_id')->select(["{$member_table}.id",
                                                           "{$member_table}.phone",
                                                           "{$member_table}.nickname",
                                                           "{$member_table}.realname",
                                                           "{$member_table}.avatar",
                                                           "{$merchant_table}.is_root"])->where("{$merchant_table}.is_root", '=', 1);
    }
}